package com.bob.clan.wxconnector.component;

import com.bob.clan.wxconnector.DTO.WebAuthorMsgDTO;
import com.bob.clan.wxconnector.entity.Fans;
import com.bob.clan.wxconnector.entity.Mp;
import com.bob.clan.wxconnector.config.SysConfig;
import com.bob.clan.wxconnector.exception.MpException;
import com.bob.clan.wxconnector.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class WebAuthorComponent {
    @Autowired
    private SysConfig sysConfig;

    //获取userBase，传wsSessionId，用于老用户扫码登录
    public String getUserBaseUrl(Mp mp, String sessionId){
        String redirectUrl = sysConfig.reLoginUserBaseUrl.replace("SERVER_PATH",sysConfig.getServerPath()).replace("BUS_SYS_CODE",mp.getSysCode()).replace("WS_SESSION_ID",sessionId);
        return sysConfig.WXUserBaseUrl.replace("APPID", mp.getAppid()).replace("REDIRECT_URI", redirectUrl);
    }
    //获取userInfo，传wsSessionId，用于新用户扫码登录
    public String getUserInfoUrl(Mp mp, String sessionId){
        String redirectUrl = sysConfig.reLoginUserInfoUrl.replace("SERVER_PATH",sysConfig.getServerPath()).replace("BUS_SYS_CODE",mp.getSysCode()).replace("WS_SESSION_ID",sessionId);
        return sysConfig.WXUserInfoUrl.replace("APPID", mp.getAppid()).replace("REDIRECT_URI", redirectUrl);

    }

    //获取userBase，不传wsSessionId，用于老用户页面授权登录（如通过模板消息链接时，用户群体肯定是老用户）
    public String getCommonAuthorUserBaseUrl(Mp mp){
        String redirectUrl = sysConfig.reCommonAuthorUserInfoUrl.replace("SERVER_PATH",sysConfig.getServerPath()).replace("BUS_SYS_CODE",mp.getSysCode());
        return sysConfig.WXUserBaseUrl.replace("APPID", mp.getAppid()).replace("REDIRECT_URI", redirectUrl);
    }

    //获取userInfo，不传wsSessionId，用于新用户页面授权登录（如扫码开展业务系统的活动时（如申请族谱的档案员时），用户群体可能是新用户）
    public String getCommonAuthorUserInfoUrl(Mp mp){
        String redirectUrl = sysConfig.reCommonAuthorUserInfoUrl.replace("SERVER_PATH",sysConfig.getServerPath()).replace("BUS_SYS_CODE",mp.getSysCode());
        return sysConfig.WXUserInfoUrl.replace("APPID", mp.getAppid()).replace("REDIRECT_URI", redirectUrl);
    }

    public String getTokenUrl(Mp mp, String code){
        return sysConfig.WXAccessTokenUrl.replace("APPID", mp.getAppid()).replace("SECRET", mp.getSecret()).replace("CODE", code);
    }

    private String getFansInfoURL(String token, String openid){
        return sysConfig.WXFansInfoUrl.replace("ACCESS_TOKEN",token).replace("OPENID", openid);
    }

    public WebAuthorMsgDTO getUserBaseMsg(Mp mp, String code) throws MpException, MpException {
        log.info("已进入getBaseOpenId方法");
        String sns_token_url = this.getTokenUrl(mp,code);
        log.debug("sns_user_base, get openid,url=" + sns_token_url);
        String respXML = HttpUtil.reqJsonGet(sns_token_url);
        log.debug("sns_user_base, get openid,respXML=" + respXML);
        JSONObject retJO = JSONObject.fromObject(respXML);
        WebAuthorMsgDTO userBaseMsg = new WebAuthorMsgDTO();
        userBaseMsg.setOpenid(retJO.getString("openid"));
        userBaseMsg.setAccess_token(retJO.getString("access_token"));
        return userBaseMsg;
    }

    public Fans getFansInfo(String token, String openid) throws MpException, MpException {
        log.info("已进入getFansInfo方法");
        String userInfoUrl = this.getFansInfoURL(token,openid);
        String respXML = HttpUtil.reqJsonGet(userInfoUrl);
        log.debug("getFansInfo, get respXML=" + respXML);
        JSONObject retJO = JSONObject.fromObject(respXML);
        Fans fans = new Fans();
        fans.setOpenid(retJO.getString("openid"));
        fans.setUnionid(retJO.getString("unionid"));
        fans.setNickname(retJO.getString("nickname"));
        fans.setHeadImgUrl(retJO.getString("headimgurl"));
        fans.setSex(retJO.getInt("sex"));
        return fans;
    }




}
